package com.example.yuwei.mediaplayer.timeutil;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class Tools {
    private long minsize = 1024 * 800;
    /**
     * 功能：扫描音频文件返回list
     *
     * @param context
     * @return musiclist
     */
    public ArrayList<Map<String, Object>> scanAllAudioFiles(Context context) {
        ArrayList<Map<String, Object>> musicList = new ArrayList<Map<String, Object>>();
        Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                null, null, null, MediaStore.Audio.Media.DEFAULT_SORT_ORDER);

        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                //音乐编号
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media._ID));
                //专辑编号
                int albumId = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
                //音乐标题
                String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
                //音乐专辑名
                String album = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM));
                //音乐歌手名
                String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                //音乐文件路径
                String url = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                //音乐总时长
                int duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
                //音乐文件大小
                long size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
                if (size > minsize) {
                    Map<String, Object> music = new HashMap<String, Object>();
                    music.put("Id", id);
                    music.put("AlbumId", albumId);
                    music.put("Title", title);
                    music.put("Album", album);
                    music.put("Artist", artist);
                    music.put("Url", url);
                    music.put("Duration", toTime(duration));
                    music.put("Size", size / (1024 * 1024));
                    musicList.add(music);
                    Log.i("musicInfo", "musicId:" + id + "title:" + title + "artist" + artist + "url:" + url + "time:" + duration);
                }
                cursor.moveToNext();
            }
        }
        if(cursor != null){
           cursor.close();
        }
        return musicList;
    }

    public String toTime(int time) {
        String string = "";
        time /= 1000;
        int minute = time / 60;
        int second = time % 60;
        int hour = minute / 60;
        minute %= 60;
        if (hour > 0) {
            if (second < 10) {
                string = hour + ":" + minute + ":0" + second;
            } else {
                string = hour + ":" + minute + ":" + second;
            }
        } else {
            if (second < 10) {
                string = minute + ":0" + second;
            } else {
                string = minute + ":" + second;
            }
        }
        //Log.i("toTime", "toTime: " + string);
        return string;
    }
}
